/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.dsl.components.*
import com.almasb.fxgl.dsl.views.*
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.input.*
import javafx.geometry.*
import javafx.scene.input.*
import javafx.scene.paint.*

/**
 * This is an example of a basic FXGL game application.
 *
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class ScrollingBackgroundSample : GameApplication() {
	private lateinit var player: Entity
	
	override fun initSettings(settings: GameSettings) {
		settings.isManualResizeEnabled = true
		settings.isPreserveResizeRatio = true
	}
	
	override fun initInput() {
		
		getInput().addAction(object : UserAction("Move Right") {
			override fun onAction() {
				player.translateX(10.0)
			}
		}, KeyCode.D)
		
		getInput().addAction(object : UserAction("Move Left") {
			override fun onAction() {
				player.translateX(-10.0)
			}
		}, KeyCode.A)
		
		onKeyDown(KeyCode.F, "shake") { getGameScene().viewport.shakeTranslational(5.0) }
	}
	
	override fun initGame() {
		getGameScene().paddingTop.fill = Color.YELLOW
		getGameScene().paddingBot.fill = Color.RED
		getGameScene().paddingRight.fill = Color.GREEN
		getGameScene().paddingLeft.fill = Color.BLUE
		
		
		//getGameScene().getRoot().setBackground(new Background(new BackgroundFill(Color.GREEN, null, null)));
		//getGameScene().getRoot().setStyle("-fx-background-color: black");
		
		
		player = entityBuilder()
			.buildAndAttach()
		
		//        entityBuilder()
		//                .at(410, 10)
		//                .with(new OffscreenPauseComponent())
		//                .with(new DebugComponent())
		//                .viewWithBBox(new Rectangle(20, 20))
		//                .buildAndAttach();
		
		getGameScene().viewport.bindToEntity(player, 0.0, 0.0)
		getGameScene().viewport.isLazy = true
		
		entityBuilder()
			.view(ScrollingBackgroundView(getAssetLoader().loadTexture("bg_wrap.png", 1066.0, 600.0),
				Orientation.HORIZONTAL))
			.zIndex(-1)
			.buildAndAttach()
		
		// 1. load texture to be the background and specify orientation (horizontal or vertical)
		//getGameScene().addGameView();
	}
	
	// TODO: debug messages console dev pane
	internal class DebugComponent : AccumulatedUpdateComponent(10) {
		
		override fun onAccumulatedUpdate(tpfSum: Double) {
			//System.out.println(tpfSum);
		}
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(ScrollingBackgroundSample::class.java, args)
}
